/*
 * @Author: viivLgr 
 * @Date: 2018-03-25 22:12:18 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-26 10:34:34
 */
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide = require('page/common/nav-side/index.js');
var _util = require('util/util.js');
var _order = require('service/order-service.js');
var templateIndex = require('./index.string');

var page = {
    data: {
        orderNum: _util.getUrlParam('orderNum')
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        navSide.init({
            name: 'order-list'
        });
        // 加载订单列表
        this.loadOrderDetail();
    },
    bindEvent: function(){
        var _this = this;
        $(document).on('click', '.order-cancel', function(){
            if(window.confirm('确定要取消该订单？')){
                _order.cancel(_this.data.orderNum, function(res){
                    _util.successTips('该订单已取消成功');
                    _this.loadOrderDetail();
                },function(err){
                    _util.errorTips(err);
                });
            }
        });
    },
    loadOrderDetail(){
        var html = '',
            _this = this,
            $content = $('.content');
        $content.html('<div class="loading"></div>');
        _order.getOrderDetail(_this.data.orderNum, function(res){
            // 渲染HTML
            _this.dataFilter(res);
            html = _util.renderHtml(templateIndex, res);
            $content.html(html);
        }, function(err){
            $content.html('<p class="error-tip">' + err + '</p>');
        });
    },
    dataFilter: function(data){
        data.needPay = data.status == 10;
        data.isCancelable = data.status == 10;
    }
};

$(function () {
    page.init();
});
